#include "hole.h"

#include <QBrush>
#include <QPen>
#include <QPainter>
#include <QGraphicsSceneDragDropEvent>

#include "peg.h"

Hole::Hole(int col, int row, qreal radius)
    : m_col(col), m_row(row), m_peg(NULL),
      m_radius(radius)
{
    qreal diameter = m_radius * 2;
    setPos(m_col * diameter * 1.5 + m_radius,
           m_row * diameter * 1.5 + m_radius);

    // holes are always below pegs
    setZValue(-1);
}

QRectF Hole::boundingRect() const
{
    const qreal diameter = 2 * m_radius;
    return QRectF(-m_radius - m_outline_width / 2,
                  -m_radius - m_outline_width / 2,
                  diameter + m_outline_width,
                  diameter + m_outline_width);
}

void Hole::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QBrush brush(Qt::black, Qt::Dense6Pattern);
    QPen pen(Qt::SolidLine);

    pen.setWidth(m_outline_width);
    painter->setBrush(brush);
    painter->setPen(pen);
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->drawEllipse(boundingRect());
}

QPainterPath Hole::shape() const
{
    QPainterPath path;
    path.addEllipse(boundingRect());
    return path;
}

qreal Hole::radius() const
{
    return m_radius;
}

QPoint Hole::boardPos() const
{
    return QPoint(m_col, m_row);
}

Peg *Hole::setPeg(Peg *peg)
{
    Peg *old_peg = m_peg;
    m_peg = peg;
    return old_peg;
}

bool Hole::havePeg() const
{
    return m_peg != NULL;
}
